import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;


public class GameRunThread extends Thread {
	public GameScreen gs;
	public boolean running;
	public Move curMove;
	
	//time task
	Tick tick;
	
	public GameRunThread(GameScreen gs)
	{
		this.gs = gs;
		running = true;
	}
	
	class Tick extends TimerTask
	{

		int curTime;
		GameScreen gs;
		
		public Tick(GameScreen gs)
		{
			this.gs = gs;
			curTime = 0;
		}
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			gs.timer.setText("" + ++curTime);
		}
		
		public void resetTime()
		{
			curTime = 0;
		}
		
	}
	
	public void run()
	{
		//start timer
		Timer timer = new Timer();
		//instantiate time task
		tick = new Tick(gs);
		timer.schedule(tick, 0, 1000);
		
		//while the game is not done
		while (gs.gameEnded == false)
		{
			//player turn
			if (gs.turn == 1)
			{
				//if the user clicked the button
				if (gs.moveButtonClicked == true)
				{
					//reset the time
					tick.resetTime();
					
					String fd = gs.firstDot.getText();
					String sd = gs.secondDot.getText();
					if (fd.equals("") == false && sd.equals("") == false)
					{
						int result = gs.game.board.move(Integer.parseInt(fd), Integer.parseInt(sd), 'P');
						
						gs.history.setText(gs.history.getText() + "\nPlayer moves: " + fd + " " + sd);
						
						//if the player can't move, let the player move again and tell the player
						if (result == -1)
						{
							gs.message.setText("Can't move. Please make your move again");
							gs.moveButtonClicked = false;
						}
						else if (result == 1)
						{
							gs.message.setText("You won a box. You can move again");
							gs.history.setText(gs.history.getText() + "\nPlayer wins a box");
							
							//initialize the environment for the player to move
							gs.firstDot.setText("");
							gs.firstDot.setEnabled(true);
							gs.moveButton.setEnabled(true);
							gs.moveButtonClicked = false;
	
							gs.secondDot.setText("");
							gs.secondDot.setEnabled(true);
							
							//if the player won the box, check is the game ended?
							if (gs.game.board.winner() != 0) //have a winner
								gs.gameEnded = true;
						}
						else
						{
							gs.turn *= -1;
							gs.moveButtonClicked = false;
							gs.firstDot.setEnabled(false);
							gs.secondDot.setEnabled(false);
							gs.moveButton.setEnabled(false);
							gs.message.setText("Computer is thinking. Please wait");
						}
						gs.drawingFrame.repaint();
					}
				}
			}
			else //computer move
			{
				int result = 0;
				
				//the move of computer
				
				//computer move algorithm
				
				//run the algoritm thread
				AlgorithmThread at = new AlgorithmThread(this);
				
				//start the thread
				at.start();
				
				//sleep for 59s then stop the thread doing the algorithm
				try
				{
					Thread.sleep(59000);
				}
				catch (InterruptedException ex)
				{
				}
				
				//wake up, signal the thread to stop
				at.stop = true;
				//if the thread returned a move, use that move
				if (this.curMove != null)
				{
					result = gs.game.board.move(this.curMove.fDot, this.curMove.sDot, 'C');
				}
				else //else, move random
				{
					Random rand = new Random();
					ArrayList<Move> moves = gs.game.board.getAvailableMoves();
					int r = rand.nextInt(moves.size());
					
					result = gs.game.board.move(moves.get(r).fDot, moves.get(r).sDot, 'C');
				}
				
				tick.resetTime();
				
				//release the thread
				at = null;
				
				//save to history box
				gs.history.setText(gs.history.getText() + "\nComputer moves:" + curMove.fDot + " " + curMove.sDot);
				
				//if computer made the move and didn't win, player move
				if (result == 0)
				{
					//initialize the environment for the player to move
					gs.firstDot.setText("");
					gs.firstDot.setEnabled(true);
					gs.moveButton.setEnabled(true);
					gs.moveButtonClicked = false;
				
					gs.secondDot.setText("");
					gs.secondDot.setEnabled(true);
					gs.message.setText("Please make your move");
					
					gs.turn *= -1;
				}
				else //if the computer get the box, continue to move
				{
					gs.history.setText(gs.history.getText() + "\nComputer wins a box");
					gs.moveButtonClicked = false;
					//if the computer won the box, check is the game ended?
					if (gs.game.board.winner() != 0) //have a winner
						gs.gameEnded = true;
				}
				
				//after the move, set move to null
				curMove = null;
				
				gs.repaint();
			}
			
			if (gs.game.board.winner() != 0)
			{
				gs.gameEnded = true;
			}
		}
		
		int winner = gs.game.board.winner();
		
		if (winner == 1)
		{
			//player wins
			JOptionPane.showMessageDialog(gs, "Congratulations! You win");
		}
		else if (winner == -1)
		{
			//computer wins
			JOptionPane.showMessageDialog(gs, "The AI wins");
		}
		else
		{
			//it's a draw
			JOptionPane.showMessageDialog(gs, "It's a draw");
		}
	}
}
